package com.esyoga.yoyoga.handler;

import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.service.EvaluationSevice;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/evaluation")
public class EvaluationHandler {

    @Resource
    EvaluationSevice evaluationSevice;

    /**
     * 根据订单id获取订单详情,以及教练信息
     * @param oId
     * @return
     */
    @RequestMapping("/getOrderInfoByOid")
    @ResponseBody
    public Map<String,Object> getOrderInfoByOId(int oId){
        TOrder order = evaluationSevice.getOrderByOId(oId);
        int cId = order.getoCId();
        TContract contract = evaluationSevice.getContractByCId(cId);
        int tId = contract.getcTId();
        TTrainer trainer = evaluationSevice.getTrainerByTId(tId);
        Map<String,Object> orderInfo = new Hashtable<>();
        orderInfo.put("order",order);
        orderInfo.put("trainer",trainer);
        List<TEvaluation> evaluations = evaluationSevice.getEvaluationByTId(tId);
        double eventStar = 0.0;
        if (evaluations.size()!=0){
            double sum = 0;
            for (TEvaluation evaluation:evaluations){
                sum += evaluation.geteStar();
            }
            eventStar = sum/evaluations.size();
        }
        int eventStarInt=(int)eventStar;
        if ((eventStar-0.5)>eventStar){
            eventStar = eventStarInt+0.5;
        }else {
            eventStar = eventStarInt;
        }
        orderInfo.put("eventStar", eventStar);
        return orderInfo;
    }

    /**
     * 用于添加订单
     * @param evaluation    订单信息
     * @return
     */
    @RequestMapping("/addEvaluation")
    @ResponseBody
    public String addEvaluation(TEvaluation evaluation, HttpServletRequest request){
        TStudent student = (TStudent) request.getSession().getAttribute("tStudent");
        evaluation.seteSId(student.getsId());
//        evaluation.seteSId(1);
        int row = evaluationSevice.addEvaluation(evaluation);
        if (row>0)return "修改成功";
        return "修改失败";
    }
}
